package com.lvmama.monkey.common.utils

import java.io.File
import java.text.SimpleDateFormat
import java.util.Calendar


/**
  * Created by Administrator on 2017/10/12.
  */
object Utils {

  /**
    * 获取昨天的时间戳
    *
    * @return
    */
  def getYesterdayTimeInMillis(): Long= {
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, -1)
    cal.set(Calendar.HOUR ,0)
    cal.set(Calendar.HOUR_OF_DAY ,0)
    cal.set(Calendar.MINUTE ,0)
    cal.set(Calendar.SECOND ,0)
    cal.getTimeInMillis
  }

  def getLastHourDate(format: String = "yyyy/MM/dd/HH"): String= {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.HOUR, -1)
    val yesterday = dateFormat.format(cal.getTime)
    yesterday
  }

  def getToday(format: String = "yyyyMMdd"): String= {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    val cal: Calendar = Calendar.getInstance()
    val today = dateFormat.format(cal.getTime)
    today
  }

  def getYesterday(format: String = "yyyyMMdd"): String= {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, -1)
    val yesterday = dateFormat.format(cal.getTime)
    yesterday
  }

  def getTime(format: String = "yyyy-MM-dd:HH"): String = {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    val cal: Calendar = Calendar.getInstance()
    val time = dateFormat.format(cal.getTime)
    time
  }

  def timeFormat(time: Long, format: String = "yyyy-MM-dd:HH"): String = {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    dateFormat.format(time)
  }

  def date2Stamp(s: String, format: String = "yyyy-MM-dd HH:mm:ss"): Long = {
    val simpleDateFormat = new SimpleDateFormat(format)
    val date= simpleDateFormat.parse(s)
    date.getTime
  }

  def deleteAllFilesOfDir(path: File){
    if (!path.exists())
      return
    if (path.isFile) {
      path.delete()
      return
    }
    val files = path.listFiles()
    for (file <- files) {
      deleteAllFilesOfDir(file)
    }
    path.delete()
  }

}